package com.commons;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;

public class Converter {
	/*
	 * 该类主要用来实现获得获得客户端的参数并赋给相应的vo对象
	 */
public static <T> T convert(Class<T> clazz,HttpServletRequest request) {
	T obj=null;
	try {
		obj=clazz.newInstance();
		Field[] fs=clazz.getDeclaredFields();
		for(Field f:fs) {
			f.setAccessible(true);
			String name=f.getName();
			if(request.getParameter(name)!=null) {
				if(f.getType().isArray()) {//数组的处理还没有完成
					f.set(obj, request.getParameter(name));
				}else {
					if(f.getType()==String.class) {
						f.set(obj, request.getParameter(name));
					}else if(f.getType()==int.class||f.getType()==Integer.class) {
						f.set(obj, Integer.valueOf(request.getParameter(name)));
					}else if(f.getType()==long.class||f.getType()==Long.class) {
						f.set(obj, Long.valueOf(request.getParameter(name)));
					}else if(f.getType()==float.class||f.getType()==Float.class) {
						f.set(obj, Float.valueOf(request.getParameter(name)));
					}else if(f.getType()==double.class||f.getType()==Double.class) {
						f.set(obj, Double.valueOf(request.getParameter(name)));
					}
				}
			}
		}
	}catch(InstantiationException e) {
		e.printStackTrace();
	}catch(IllegalAccessException e) {
		e.printStackTrace();
	}
	
	
	
	
	return obj;
}
}
